package com.luweijie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luweijie.entity.Mountings;
import com.luweijie.entity.Order;
import com.luweijie.mapper.CalculatorBackgroundMapper;
import com.luweijie.mapper.CalculatorMapper;
import com.luweijie.mapper.CalculatorOrderMapper;
import com.luweijie.service.CalculatorBackgroundService;
import com.luweijie.service.CalculatorService;
import com.luweijie.utils.JsonUtils;
import org.aspectj.weaver.ast.Var;
import org.bouncycastle.crypto.params.CCMParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.soap.Addressing;
import java.util.List;
import java.util.Map;

@Service
public class CalculatorServiceImpl extends ServiceImpl<CalculatorMapper, Mountings> implements CalculatorService {

       @Autowired
       CalculatorMapper calculatorMapper;
       @Autowired
       CalculatorOrderMapper calculatorOrderMapper;
        @Override
        public double Calculator(Order order) {
            List<String> categorys = order.getCategorys();
            double totalPrice = 0;
            for (String body : categorys) {
                //解析json
                Map<String, Object> data = JsonUtils.stringToMap(body);
                String body1 = String.valueOf(data.get("body"));
                Map<String, Object> category = JsonUtils.stringToMap(body1);
                String category1 = String.valueOf(category.get("category"));
                double quantity1 = Double.valueOf((Double) category.get("quantity"));
                QueryWrapper<Mountings> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("category",category1);
                Mountings mountings = calculatorMapper.selectOne(queryWrapper);
                double price = mountings.getPrice();
                 totalPrice=  price * quantity1;
            }
            return totalPrice;
        }

    @Override
    public boolean insters(Order order) {
        return calculatorOrderMapper.insert(order)>0;
    }
}
